﻿/*
    Autor : Roger Medeiros
    Data  : 01/02/2012
    Site  : www.interacaosistemas.com.br
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Registro = Microsoft.Win32;
using System.Xml;
using System.IO;

namespace i9PgAdmin.Core
{
    public class Conexao
    {
        public string Servidor { get; set; }
        public int Porta { get; set; }
        public string Usuario { get; set; }
        public string Senha { get; set; }
        public string Database { get; set; }                
        public string CaminhoBin { get; set; }
        public string CaminhoDump { get; set; }
        public bool Comprimir { get; set; }

        private string Arquivo { get; set; }

        public Conexao()
        {
            this.Arquivo = "i9PgAdmin.config";
            this.Servidor = "localhost";
            this.Porta = 5432;
            this.Usuario = "postgres";
            this.Senha = string.Empty;
            this.Database = string.Empty;
            this.CaminhoBin = string.Empty;
            this.CaminhoDump = string.Empty;
            this.Comprimir = true;
            this.PegaCaminhoBin();
        }

        public void PegaCaminhoBin()
        {
            var root = Registro.Registry.LocalMachine.OpenSubKey("SOFTWARE\\PostgreSQL\\Installations");
            if (root != null)
            {
                foreach (string versao in root.GetSubKeyNames())
                {
                    var child = root.OpenSubKey(versao);
                    this.CaminhoBin = child.GetValue("Base Directory").ToString() + "\\bin\\";
                    return;
                }
            }
        }

        public void Carrega()
        {
            if (!File.Exists(this.Arquivo))
            {
                this.Salva();
            }
            
            XmlTextReader config = new XmlTextReader(this.Arquivo);
            try
            {
                while (config.Read())
                {
                    if (config.Name == "Servidor")
                        this.Servidor = config.ReadElementString().Trim();
                    if (config.Name == "Usuario")
                        this.Usuario = config.ReadElementString().Trim();
                    if (config.Name == "Senha")
                        this.Senha = config.ReadElementString().Trim();
                    if (config.Name == "Database")
                        this.Database = config.ReadElementString().Trim();
                    if (config.Name == "Porta")
                        this.Porta = int.Parse(config.ReadElementString().Trim());
                    if (config.Name == "CaminhoDump")
                        this.CaminhoDump = config.ReadElementString().Trim();
                    if (config.Name == "Comprimir")
                        this.Comprimir = (config.ReadElementString().Trim() == "S");
                    if (config.Name == "CaminhoBin")
                        this.CaminhoBin = config.ReadElementString().Trim();
                }
                config.Close();
            }
            catch
            {
                config.Close();
                this.Salva();
                this.Carrega();
            }
        }

        public void Salva()
        {
            if (File.Exists(this.Arquivo))
            {
                File.Delete(this.Arquivo);
            }

            XmlTextWriter config = new XmlTextWriter(this.Arquivo, null);
            config.WriteStartDocument();

            config.WriteStartElement("configuracao");

            config.WriteElementString("Servidor", this.Servidor.Trim());
            config.WriteElementString("Usuario", this.Usuario.Trim());
            config.WriteElementString("Senha", this.Senha.ToString());
            config.WriteElementString("Database", this.Database.ToString());
            config.WriteElementString("Porta", this.Porta.ToString());
            config.WriteElementString("CaminhoDump", this.CaminhoDump.ToString());
            config.WriteElementString("Comprimir", (this.Comprimir ? "S" : "N"));
            config.WriteElementString("CaminhoBin", this.CaminhoBin.ToString());

            config.WriteEndElement();
            config.WriteEndDocument();

            config.Flush();
            config.Close();
        }
    }
}
